package com.wxc.wxcwzq.core.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wxc.wxcwzq.core.entity.Employee;
import com.wxc.wxcwzq.core.service.EmployeeService;
import com.wxc.wxcwzq.core.util.ResultDao;
import com.wxc.wxcwzq.core.util.ReturnUtil;
import com.wxc.wxcwzq.web.entity.SysUser;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Controller
@RequestMapping("/wxc/employee")
public class EmployeeController {

	@Autowired
	private EmployeeService service;
	
	@RequestMapping("/getEmplist")
	@ResponseBody
	public String getUserInfo(Employee employee,Integer page, Integer limit){
		log.info("查询用户信息，请求参数：{},{}","page:"+page,"limit:"+limit);
		Page<Employee> emplist = service.getEmployeesList(employee,page,limit);
		ResultDao result = new ResultDao();
		result.setCode(0);
		result.setMsg("失败请重试");
		result.setCount(emplist.getTotal());
		result.setData(emplist.getResult());
		return JSON.toJSONString(result);
	}
	
	@RequestMapping("/addPage")
	@RequiresPermissions("userInfo:add")
	public ModelAndView addPage(){
		return new ModelAndView("page/user/userAdd");
	}
	
	@RequestMapping("/addUsers")
	@ResponseBody
	public String employeeAdd(Employee employee){
		ReturnUtil result = new ReturnUtil();
		try {
			Integer res = service.addOrUpdate(employee);
			if(res==1){
				result.setResult(true);
			}else{
				result.setResult(false);
				result.setMsg("网络异常，保存失败！");
			}
		} catch (Exception e) {
			result.setResult(false);
			result.setMsg("网络异常，保存失败！");
		}
		return JSON.toJSONString(result);
	}
	/**
	 * 删除用户
	 * @param employee
	 * @return
	 */
	@RequestMapping("/deleteUsers")
	@ResponseBody
	public String deleteUsers(Long id){
		log.info("删除用户，请求参数：{}","id:"+id);
		ReturnUtil result = new ReturnUtil();
		try {
			Integer res = service.deleteUserById(id);
			if(res==1){
				result.setResult(true);
			}else{
				result.setResult(false);
				result.setMsg("数据异常，请刷新后重试！");
			}
		} catch (Exception e) {
			result.setResult(false);
			result.setMsg("网络异常，请刷新后重试！");
		}
		return JSON.toJSONString(result);
	}
	
}
